<template>
  <div class="app-container">
    <!-- 添加权限 -->
    <el-card>
      <el-row type="flex" justify="end">
        <el-button
          type="success"
          size="small"
          icon="el-icon-edit"
          @click="addPermission"
          >新增学科</el-button
        >
      </el-row>
      <!-- 表单 -->
      <el-table
        :data="list"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'childs', hasChildren: 'points' }"
      >
        <el-table-column label="标题" width="200" prop="title">
          <template v-slot="{ row }">
            <i v-if="row.pid === null" class="el-icon-folder-opened"></i>
            <i v-else-if="row.is_point" class="el-icon-view"></i>
            <i v-else class="el-icon-document"></i>
            <span>{{ row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="权限代码"
          width="850"
          prop="code"
        ></el-table-column>
        <el-table-column label="操作" width="100">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              circle
              plain
              @click="editPermission(row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              circle
              plain
              @click="delMun(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增编辑弹层 -->
    <el-dialog :visible="showDialog" title="创建菜单" @close="btnCencel">
      <!-- 表单 -->
      <el-form
        ref="permForm"
        :model="formDate"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="权限组名称" prop="is_point">
          <el-radio-group
            v-model="formDate.is_point"
            :disabled="formDate.id !== null"
          >
            <el-radio :label="false">菜单</el-radio>
            <el-radio :label="true">权限点</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限组名称" prop="pid  ">
          <el-select v-model="value" style="width: 40%" ref="options">
            <el-option
              v-for="item in result"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              style="display:none"
            ></el-option>
            <el-tree
              :data="result"
              :props="defaultProps"
              default-expand-all
              node-key="id"
              :expand-on-click-node="false"
              @node-click="handleNodeClick"
            ></el-tree>
          </el-select>
        </el-form-item>
        <el-form-item label="权限代码" prop="code">
          <el-input v-model="formDate.code" style="width: 60%" />
        </el-form-item>
        <el-form-item label="权限标题" prop="title">
          <el-input v-model="formDate.title" style="width: 60%" />
        </el-form-item>
      </el-form>
      <!-- 底部的的确定和取消 -->
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCencel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  meunDate,
  deleteMeun,
  addMenu,
  menuDetails,
  menuModification
} from '@/api/meun'
export default {
  name: 'Menu',
  data() {
    return {
      list: [],
      showDialog: false,
      formDate: {
        is_point: true, // 是否权限点
        pid: 0, // 父id
        code: '', // 菜单代码 全局唯一
        title: '', // 菜单标题
        id: null
      },
      rules: {
        code: [{ required: true, message: '权限代码不能为空', tigger: 'blur' }],
        title: [{ required: true, message: '权限标题不能为空', tigger: 'blur' }]
      },
      defaultProps: {
        label: 'title',
        children: 'childs'
      },
      value: '主导航',
      result: []
    }
  },
  created() {
    this.meunDate()
  },
  methods: {
    async meunDate() {
      const { data } = await meunDate()
      this.list = JSON.parse(JSON.stringify(data).replace(/points/g, 'childs'))
      this.result.push({ title: '主导航', pid: 0, childs: this.list })
      console.log(this.list)
    },
    delMun(id) {
      console.log(id)
      this.$confirm('确认删除该标题嘛')
        .then(() => {
          return deleteMeun(id)
        })
        .then(() => {
          this.$message.success('删除成功')
          // 重新拉取数据 获取最新数据
          this.meunDate()
        })
    },
    addPermission() {
      this.showDialog = true
    },
    btnOk() {
      // console.log(this.formDate)
      this.$refs.permForm
        .validate()
        .then(() => {
          // 校验成功
          if (this.formDate.id) {
            // 认为是编辑
            return menuModification({
              ...this.formDate,
              is_point: this.formDate.is_point ? true : false
            })
          }
          return addMenu({
            ...this.formDate,
            is_point: this.formDate.is_point ? true : false
          }) // 新增接口
        })
        .then(() => {
          // 添加成功
          this.$message.success('操作成功')
          // 重新获取数据
          this.meunDate()
          this.showDialog = false
        })
        .catch(() => {
          console.log(11)
        })
    },
    btnCencel() {
      this.formDate = {
        is_point: true, // 是否权限点
        pid: 0, // 父id
        code: '', // 菜单代码 全局唯一
        title: '', // 菜单标题
        id: null
      }
      this.$refs.permForm.resetFields()
      this.showDialog = false
    },
    async editPermission(id) {
      this.showDialog = true
      const data = await menuDetails(id)
      this.formDate = data.data
    },
    handleNodeClick(node) {
      console.log(node)
      this.value = node.title
      console.log(this.value)
      this.formDate.pid = node.id
      this.$refs.options.blur()
    }
  }
}
</script>
<style scoped>
div /deep/ .el-dialog__header {
  background-color: #409eff;
}
.el-dialog__title {
  color: #fff;
}
.el-dialog__close {
  color: #fff;
}
div /deep/ .el-icon-caret-right:before {
  display: none;
}
</style>
